package com.nclg.exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nclg.exam.dto.*;
import com.nclg.exam.mapper.UserPaperMapper;
import com.nclg.exam.service.UserPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserPaperServiceImpl implements UserPaperService {

    @Autowired
    private UserPaperMapper userPaperMapper;

    /**
     * 查询阅卷人
     * @return
     */
    @Override
    public List<CreaterDto> selectExaminers() {
        return userPaperMapper.selectExaminers();
    }

    /**
     * 查询学生考卷
     * @param dto
     * @return
     */
    @Override
    public PageInfo<UserPaperListDto> selectByPage(UserPaperDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<UserPaperListDto> list = userPaperMapper.selectByPage(dto);
        return new PageInfo<>(list);
    }

    /**
     * 查询考卷
     * @param upId
     * @return
     */
    @Override
    public UserPaperFinishedDto selectById(Long upId) {
        return userPaperMapper.selectById(upId);
    }

    /**
     * 阅卷
     * @param dto
     * @return
     */
    @Override
    public boolean updateReviewPaper(ReviewPaperDto dto) {
        boolean isTrue = userPaperMapper.updateUserAnswerByUpid(dto.getUpid(), dto.getAnswers()) > 0 ? true : false;
        if (!isTrue) return false;
        return userPaperMapper.updateById(dto.getUpid(), dto.getAid(), dto.getScore()) > 0 ? true : false;
    }
}
